<script setup lang="ts">
import { useDark, useECharts } from "@pureadmin/utils";
import { type PropType, ref, computed, watch, nextTick } from "vue";

const props = defineProps({
  deliveryOrder: {
    type: Array as PropType<Array<number>>,
    default: () => []
  },
  dispatchOrder: {
    type: Array as PropType<Array<number>>,
    default: () => []
  },
  storageOrder: {
    type: Array as PropType<Array<number>>,
    default: () => []
  }
});

const { isDark } = useDark();

const theme = computed(() => (isDark.value ? "dark" : "light"));

const chartRef = ref();
const { setOptions } = useECharts(chartRef, {
  theme
});

watch(
  () => props,
  async () => {
    await nextTick();
    setOptions({
      container: ".bar-card",
      color: ["#41b6ff", "#e85f33"],
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "none"
        }
      },
      grid: {
        top: "20px",
        left: "50px",
        right: 0
      },
      legend: {
        data: ["发货订单数", "存货订单数", "提货订单数"],
        textStyle: {
          color: "#606266",
          fontSize: "0.875rem"
        },
        bottom: 0
      },
      xAxis: [
        {
          type: "category",
          data: [
            "第一周",
            "第二周",
            "第三周",
            "第四周",
            "第五周",
            "第六周",
            "本周"
          ],
          axisLabel: {
            fontSize: "0.875rem"
          },
          axisPointer: {
            type: "shadow"
          }
        }
      ],
      yAxis: [
        {
          type: "value",
          axisLabel: {
            fontSize: "0.875rem"
          },
          splitLine: {
            show: false // 去网格线
          }
        }
      ],
      series: [
        {
          name: "发货订单数",
          type: "bar",
          barWidth: 15,
          itemStyle: {
            color: "#47c8ff",
            borderRadius: [10, 10, 0, 0]
          },
          data: props.deliveryOrder
        },
        {
          name: "存货订单数",
          type: "bar",
          barWidth: 15,
          itemStyle: {
            color: "#ff6838",
            borderRadius: [10, 10, 0, 0]
          },

          data: props.dispatchOrder
        },
        {
          name: "提货订单数",
          type: "bar",
          barWidth: 15,
          itemStyle: {
            color: "#63ba7e",
            borderRadius: [10, 10, 0, 0]
          },

          data: props.storageOrder
        }
      ]
    });
  },
  {
    deep: true,
    immediate: true
  }
);
</script>

<template>
  <div ref="chartRef" style="width: 100%; height: 500px" />
</template>
